/*
 * Copyright (C) 2011 AaDevelopment Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aa.AaCamera_Host_Demo;


import java.lang.reflect.Method;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.util.Log;


public class HostUtil 
{
	//GENERAL DEF
	public static final String ACTION_DO_COUNT = "aa.AaCamera.intent.action.DO_COUNT_V1" ;
	public static final int DO_CAMERA_COUNT_REQUEST_CODE = 0x8665422 ;
	public static final int CURRENT_INTERFACE_VERSION    = 0x1;  

	public enum  Flags 
	{
		//Flags DEF
		// when set count will down to 0 starting from EXTRA_IN_INT_EXERCISE_MAX_COUNT
		// When clear count on screen will be counting up staring from 0
		FLAG_IN_COUNT_DOWN 	                   ( 0x000001),

		//When set works silently
		FLAG_IN_SILENT                         ( 0x000002 ), 

		//Group of Exit reason flags
		FLAG_OU_EXIT_DUE_INACTIVITY_TIMEOUT    ( 0x000004 ),
		FLAG_OU_EXIT_DUE_EXERCISE_MAX_COUNT    ( 0x000008 ),
		FLAG_OU_EXIT_DUE_EXERCISE_MAX_TIME     ( 0x000010 );
		//	FLAG_OU_EXIT_DUE_USER_REQUEST          ( 0x000020 );
		private final int mValue;

		Flags (int val)
		{
			this.mValue = val;
		}
		//
		public int get ()
		{
			return mValue;
		}
	}//flag

	//Extra DEF 
	//      _IN_  means IN parameter
	//      _OU_  means OU parameter
	//      After follows type of param _INT_ , _RECT_ , STR

	public static final String EXTRA_IN_INT_FLAGS 			=  "IN_INT_FLAGS";
	public static final String EXTRA_OU_INT_FLAGS 			=  "OU_INT_FLAGS";


	// Here will be reported result of user activity
	public static final String EXTRA_OU_INT_EXERCISE_COUNT_DONE      =  "OU_INT_EXERCISE_COUNT_DONE";

	// Here will be reported result of user activity
	// User could spend more  time on Reset or prepare proper place for camera.
	// Below will be reported clean time to calculate real exercise rate
	public static final String EXTRA_OU_INT_EXERCISE_TIME_DONE_SEC   =  "OU_INT_EXERCISE_TIME_DONE_SEC";

	// Set position on active zone on screen
	public static final String EXTRA_IN_RECT_ACTIVE_ZONE    =  "IN_RECT_ACTIVE_ZONE";

	// Active zone position after EXERCISE (user change it )  
	public static final String EXTRA_OU_RECT_ACTIVE_ZONE    =  "OU_RECT_ACTIVE_ZONE";

	// When no set Default value will taken from AaCamera setting. 
	// When there is no user activity during this timeout AaCamera will be exit 
	// with setting FLAG_OU_EXIT_DUE_INACTIVITY_TIMEOUT
	public static final String EXTRA_IN_INT_INACTIVE_TIMEOUT_SEC     =  "IN_INT_INACTIVE_TIMEOUT_MIN";

	//When no set will be not controlled.
	//When set AaCamera will exit upon count will reach this value 
	//with setting FLAG_OU_EXIT_DUE_EXERCISE_MAX_COUNT
	//When set FLAG_IN_COUNT_DOWN  this value will be set on screen counter
	public static final String EXTRA_IN_INT_EXERCISE_MAX_COUNT     =  "IN_INT_EXERCISE_MAX_COUNT";


	//When set AaCamera will exit upon this timeout will exhausted 
	//with setting FLAG_OU_EXIT_DUE_EXERCISE_MAX_TIME_SEC
	public static final String EXTRA_IN_INT_EXERCISE_MAX_TIME_SEC    =  "IN_INT_EXERCISE_MAX_TIME_MIN";


	final static Intent mAaCameraIntent = new Intent(ACTION_DO_COUNT);
	final  PackageManager mPm;
	//CTOR
	HostUtil (PackageManager pm)
	{
		mPm = pm;
		mAaCameraIntent.addFlags(				
				Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
				| Intent.FLAG_ACTIVITY_NO_HISTORY );
	}

	//-----
	static public    boolean verifyAndroidOSverion()
	{
		return  (Build.VERSION.SDK_INT >= 9 ) ;

	}
	//---
	boolean  hasFrontCamera()
	{
		//	return mPm.hasSystemFeature("android.hardware.camera.front") ;
		boolean res = false;
		try
		{	
			Method m = PackageManager.class.getMethod("hasSystemFeature",String.class);
			res = m.invoke(mPm, "android.hardware.camera.front").equals(true);

		} catch (Throwable e)
		{
			Log.d ("_Aa",  "EXception "+ e)  ;
		}
		return res;
	}
	//----- 
	boolean isAaCameraPackageInstalled()
	{

		List<ResolveInfo> list = mPm.queryIntentActivities(
				mAaCameraIntent, 
				PackageManager.MATCH_DEFAULT_ONLY);
		return  list.size() > 0;   	
	}
	//-----

	public  boolean downloadFromMarket(Context ctx)
	{
		boolean res = true;
		Intent marketAaCameraIntent ;
		marketAaCameraIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:aa.AaCamera"));
		//  	 marketAaCameraIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:aa.AaCount"));
		marketAaCameraIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		try{
			ctx.startActivity(marketAaCameraIntent); 
		}

		catch (android.content.ActivityNotFoundException e)
		{
			res = false;
		}
		return res;
	}  

}//endOfClass
//endOfFile
